package com.yy.controller;

import cn.hutool.core.date.DateUtil;
import com.yy.clientEntities.TestData;
import com.yy.utils.Result;
import com.yy.utils.ServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * Author young
 * Date 2023/1/10 16:06
 * Description: SpringCloudAlibaba
 */
@RestController
@Slf4j
@RequestMapping("sentinel")
public class SentinelTestController {
    @Autowired
    private ServiceUtil serviceUtil;
    @Value("${server.port}")
    private String port;
    @PutMapping("add")
    public Result<String> add(@RequestBody TestData testData){
        return Result.ok(serviceUtil.add(testData)).message("->端口号为"+port);
    }
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable("id")Integer id){
        return Result.ok(serviceUtil.delete(id)).message("->端口号为"+port);
    }
    @PostMapping("update")
    public Result<TestData> update(@RequestBody TestData testData){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return Result.ok(serviceUtil.update(testData)).message("->端口号为"+port);
    }
    @GetMapping("find/{id}")
    public Result<TestData> findOne(@PathVariable("id")Integer id){
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        log.info("当前时间为：{}，线程{}开始执行findOne方法", DateUtil.format(LocalDateTime.now(),"yyyy年MM月dd日 HH:mm:ss"),Thread.currentThread().getName());
        return Result.ok(serviceUtil.findOne(id)).message("->端口号为"+port);
    }
    @GetMapping("all")
    public Result<Collection<TestData>> all(){
        log.info("当前时间为：{}，线程{}开始执行findAll方法", DateUtil.format(LocalDateTime.now(),"yyyy年MM月dd日 HH:mm:ss"),Thread.currentThread().getName());
        return Result.ok(serviceUtil.findAll()).message("->端口号为"+port);
    }

}
